/**
 * 
 */
package com.whf15.o2o.service.impl;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.whf15.o2o.dao.ShopDao;
import com.whf15.o2o.dto.ImageHolder;
import com.whf15.o2o.dto.ShopExecution;
import com.whf15.o2o.entity.Shop;
import com.whf15.o2o.enums.ShopStateEnum;
import com.whf15.o2o.exceptions.ShopOperationException;
import com.whf15.o2o.service.ShopService;
import com.whf15.o2o.util.ImageUtil;
import com.whf15.o2o.util.PageCalculator;
import com.whf15.o2o.util.PathUtil;

/**
 * @author 作者 :whfo
 * @version 创建时间：2021年5月3日 下午4:06:32 类说明
 */
@Service
public class ShopServiceImpl implements ShopService {
	@Autowired
	private ShopDao shopDao;


	/**
	* 创建人：whfo   
	* 创建时间：2021年7月4日 下午10:07:39
	* 将原来方法中的两个参数InputStream Image和String ImageName封装到dto中
	* @param shop
	* @param thumbnail
	* @return
	* @throws ShopOperationException 
	* @version v1.1
	*/
	@Override
	@Transactional
	public ShopExecution addShop(Shop shop,ImageHolder thumbnail)
			throws ShopOperationException {
		// 商铺信息是否为空
		if (shop == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}
		// 区域等信息是否为空......
		try {
			// 往数据库增加信息
			// 给店铺信息赋初始值
			shop.setEnableStatus(0);
			shop.setCreateTime(new Date());
			shop.setLastEditTime(new Date());
			// 添加店铺信息
			int effectedNum = shopDao.insertShop(shop);
			if (effectedNum <= 0) {
				throw new ShopOperationException("店铺创建失败");
			} else {
				if (thumbnail.getImage() != null) {
					// 存储图片
					try {
						addShopImg(shop, thumbnail);
					} catch (Exception e) {
						throw new ShopOperationException("addShopImg error:" + e.getMessage());
					}
					// 更新店铺的图片地址
					effectedNum = shopDao.updateShop(shop);
					if (effectedNum <= 0) {
						throw new ShopOperationException("更新图片地址失败");
					}
					// shop.getShopImg();
				}
			}
		} catch (Exception e) {
			throw new ShopOperationException("addShop error" + e.getMessage());
		}
		return new ShopExecution(ShopStateEnum.CHECK, shop);
	}

	/**
	 *获取shop图片目录的相对值路径
	* 方法描述：  将原来方法中的两个参数InputStream Image和String ImageName封装到dto中
	* 创建人：whfo   
	* 创建时间：2021年7月4日 下午10:06:57 
	* @param shop
	* @param thumbnail
	*/
	private void addShopImg(Shop shop, ImageHolder thumbnail) {
		// 获取shop图片目录的相对值路径
		String dest = PathUtil.getShopImagePath(shop.getShopId());
		String shopImgAddr = ImageUtil.generateThumbnail(thumbnail, dest);
		shop.setShopImg(shopImgAddr);
	}

	@Override
	public Shop getByShopId(long shopId) {
		return shopDao.queryByShopId(shopId);
	}


	/**
	* 创建人：whfo   
	* 创建时间：2021年7月4日 下午9:59:37   
	* @version    
	* 将原来方法中的两个参数InputStream Image和String ImageName封装到dto中
	* 重构代码，减少参数数量
	*/
	@Override
	public ShopExecution modifyShop(Shop shop, ImageHolder thumbnail)
			throws ShopOperationException {

		if (shop == null || shop.getShopId() == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		} else {
			try {
				// 1.判断是否需要处理图片
				if (thumbnail.getImage() != null && thumbnail.getImageName() != null && !"".equals(thumbnail.getImageName())) {
					Shop tempShop = shopDao.queryByShopId(shop.getShopId());
					if (tempShop.getShopImg() != null) {
						ImageUtil.deleteFileOrPath(tempShop.getShopImg());
					}
					addShopImg(shop, thumbnail);
				}
				// 2.更新店铺信息
				shop.setLastEditTime(new Date());
				int effectedNum = shopDao.updateShop(shop);
				if(effectedNum <= 0) {
					return new ShopExecution(ShopStateEnum.INNER_ERROR);
				}else {
					shop = shopDao.queryByShopId(shop.getShopId());
					return new ShopExecution(ShopStateEnum.SUCCESS,shop);
				}
			} catch (Exception e) {
				throw new ShopOperationException("modifyShop error:"+e.getMessage());
			}
			
		}
	}

	/* (non-Javadoc)
	 * @see com.whf15.o2o.service.ShopService#getShopList(com.whf15.o2o.entity.Shop, int, int)
	 * 
	 * dao层只认行数rowIndex，前端只认页数pageIndex
	 * 需要转换
	 */
	@Override
	public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);
		int count = shopDao.queryShopCount(shopCondition);
		ShopExecution se = new ShopExecution();
		if(shopList != null) {
			se.setShopList(shopList);
			se.setCount(count);
		}else {
			se.setState(ShopStateEnum.INNER_ERROR.getState());
		}
				
		return se;
	}

	/**
	* 方法描述：
	* 创建人：whfo   
	* 创建时间：2021年8月24日 上午10:13:46
	* @version 
	*/
	@Override
	public ShopExecution getByEmployeeId(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}
}
